﻿using PickingGuide.Handle;
using PickingGuide.Model;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PickingGuideBoard
{
    public partial class Form2 : Form
    {
        PickingGuideHandler pickingGuideHandler;
        int QueueLength;
        int Station = 1;

        public Form2()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pickingGuideHandler = PickingGuideHandler.getInstance();
            var systemConfig = pickingGuideHandler.GetSystemConfig();
            //QueueLength = systemConfig.QueueLength;
            QueueLength = 5;
            //dgvQueue.ClearSelection();
            //dgvQueue.AutoGenerateColumns = false;
            //dgvQueue.ReadOnly = true;
            //dgvQueue.AddColumn("连番", "Sequence", 15);
            //dgvQueue.AddColumn("车身码", "BodyNum", 20);
            //dgvQueue.AddColumn("VIN", "VIN", 45);
            //dgvQueue.AddColumn("车型", "CarModel", 25);
            ////dgvQueue.AddColumn("接收时间", "CreatedDate", 40);
            //
            //var rowHeight = dgvQueue.Height / (QueueLength + 1);
            //
            //dgvQueue.RowHeight = rowHeight;
            //dgvQueue.ColumnHeadersHeight = rowHeight;

            //设置Timer1控件可用
            this.timer1.Enabled = false;
            //设置Timer1 时间间隔
            this.timer1.Interval = 500;
            this.timer1.Start();

            uiPanel01.BackColor = Color.LightBlue;
            uiPanel01.FillColor = Color.LightBlue;
            uiPanel01.Refresh();
        }

        private void WorkGuideRefresh(QueueInfoModel model)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                uiPanelBody.Text = model.BodyNum;
                uiPanelModel.Text = model.CarModel;
            }));
        }

        private void PanelRefresh(List<RackSetupModel> rackSetupModels)
        {
            this.Invoke(new MethodInvoker(() =>
                {
                    uiPanel01.Text = rackSetupModels[0].Name;
                    uiPanel02.Text = rackSetupModels[1].Name;
                    //uiPanel03.Text = rackSetupModels[2].Name;
                    //uiPanel04.Text = rackSetupModels[3].Name;
                    //uiPanel05.Text = rackSetupModels[4].Name;

                    uiLabel01.Text = rackSetupModels[0].Code;
                    uiLabel02.Text = rackSetupModels[1].Code;
                    //uiLabel03.Text = rackSetupModels[2].Code;
                    //uiLabel04.Text = rackSetupModels[3].Code;
                    //uiLabel05.Text = rackSetupModels[4].Code;

                    if (rackSetupModels[0].Status == "1")
                    {
                        uiPanel01.BackColor = Color.FromArgb(80, 160, 255);
                        uiPanel01.FillColor = Color.FromArgb(80, 160, 255);
                    }
                    else if (rackSetupModels[0].Status == "2")
                    {
                        uiPanel01.BackColor = Color.LightGreen;
                        uiPanel01.FillColor = Color.LightGreen;
                    }
                    else
                    {
                        uiPanel01.BackColor = Color.White;
                        uiPanel01.FillColor = Color.White;
                    }
                    uiPanel01.Refresh();
                
                    if (rackSetupModels[1].Status == "1")
                    {
                        uiPanel02.BackColor = Color.FromArgb(80, 160, 255);
                        uiPanel02.FillColor = Color.FromArgb(80, 160, 255);
                    }
                    else if (rackSetupModels[1].Status == "2")
                    {
                        uiPanel02.BackColor = Color.LightGreen;
                        uiPanel02.FillColor = Color.LightGreen;
                    }
                    else
                    {
                        uiPanel02.BackColor = Color.White;
                        uiPanel02.FillColor = Color.White;
                    }
                    uiPanel02.Refresh();
                
                    //if (rackSetupModels[2].Status == "1")
                    //{
                    //    uiPanel03.BackColor = Color.FromArgb(80, 160, 255);
                    //    uiPanel03.FillColor = Color.FromArgb(80, 160, 255);
                    //}
                    //else if (rackSetupModels[2].Status == "2")
                    //{
                    //    uiPanel03.BackColor = Color.LightGreen;
                    //    uiPanel03.FillColor = Color.LightGreen;
                    //}
                    //else
                    //{
                    //    uiPanel03.BackColor = Color.White;
                    //    uiPanel03.FillColor = Color.White;
                    //}
                    //uiPanel03.Refresh();
                
                    //if (rackSetupModels[3].Status == "1")
                    //{
                    //    uiPanel04.BackColor = Color.FromArgb(80, 160, 255);
                    //    uiPanel04.FillColor = Color.FromArgb(80, 160, 255);
                    //}
                    //else if (rackSetupModels[3].Status == "2")
                    //{
                    //    uiPanel04.BackColor = Color.LightGreen;
                    //    uiPanel04.FillColor = Color.LightGreen;
                    //}
                    //else
                    //{
                    //    uiPanel04.BackColor = Color.White;
                    //    uiPanel04.FillColor = Color.White;
                    //}
                    //uiPanel04.Refresh();
                
                    //if (rackSetupModels[4].Status == "1")
                    //{
                    //    uiPanel05.BackColor = Color.FromArgb(80, 160, 255);
                    //    uiPanel05.FillColor = Color.FromArgb(80, 160, 255);
                    //}
                    //else if (rackSetupModels[4].Status == "2")
                    //{
                    //    uiPanel05.BackColor = Color.LightGreen;
                    //    uiPanel05.FillColor = Color.LightGreen;
                    //}
                    //else
                    //{
                    //    uiPanel05.BackColor = Color.White;
                    //    uiPanel05.FillColor = Color.White;
                    //}
                    //uiPanel05.Refresh();
                }));
        }

        public void DgvRefresh(List<QueueInfoModel> queue)
        {
            this.Invoke(new MethodInvoker(() =>
            {
                //dgvQueue.DataSource = null;
                //dgvQueue.DataSource = queue;
                //Dgv_Cells(ref dgvQueue);
                /******************
                uiPanel01.Text = queue[0].Sequence.ToString("000");
                uiPanel02.Text = queue[1].Sequence.ToString("000");
                uiPanel03.Text = queue[2].Sequence.ToString("000");
                uiPanel04.Text = queue[3].Sequence.ToString("000");
                uiPanel05.Text = queue[4].Sequence.ToString("000");
                if (queue[0].Status == "ok")
                {
                    uiPanel01.BackColor = Color.LightGreen;
                    uiPanel01.FillColor = Color.LightGreen;
                }
                else if (queue[0].Status == "work")
                {
                    uiPanel01.BackColor = Color.Yellow;
                    uiPanel01.FillColor = Color.Yellow;
                }
                else if (queue[0].Status == "unok")
                {
                    uiPanel01.BackColor = Color.Red;
                    uiPanel01.FillColor = Color.Red;
                }
                else if (queue[0].Status == "pass")
                {
                    uiPanel01.BackColor = Color.LightGray;
                    uiPanel01.FillColor = Color.LightGray;
                }
                else
                {
                    uiPanel01.BackColor = Color.White;
                    uiPanel01.FillColor = Color.White;
                }
                uiPanel01.Refresh();

                if (queue[1].Status == "ok")
                {
                    uiPanel02.BackColor = Color.LightGreen;
                    uiPanel02.FillColor = Color.LightGreen;
                }
                else if (queue[1].Status == "work")
                {
                    uiPanel02.BackColor = Color.Yellow;
                    uiPanel02.FillColor = Color.Yellow;
                }
                else if (queue[1].Status == "unok")
                {
                    uiPanel02.BackColor = Color.Red;
                    uiPanel02.FillColor = Color.Red;
                }
                else if (queue[1].Status == "pass")
                {
                    uiPanel02.BackColor = Color.LightGray;
                    uiPanel02.FillColor = Color.LightGray;
                }
                else
                {
                    uiPanel02.BackColor = Color.White;
                    uiPanel02.FillColor = Color.White;
                }
                uiPanel02.Refresh();

                if (queue[2].Status == "ok")
                {
                    uiPanel03.BackColor = Color.LightGreen;
                    uiPanel03.FillColor = Color.LightGreen;
                }
                else if (queue[2].Status == "work")
                {
                    uiPanel03.BackColor = Color.Yellow;
                    uiPanel03.FillColor = Color.Yellow;
                }
                else if (queue[2].Status == "unok")
                {
                    uiPanel03.BackColor = Color.Red;
                    uiPanel03.FillColor = Color.Red;
                }
                else if (queue[2].Status == "pass")
                {
                    uiPanel03.BackColor = Color.LightGray;
                    uiPanel03.FillColor = Color.LightGray;
                }
                else
                {
                    uiPanel03.BackColor = Color.White;
                    uiPanel03.FillColor = Color.White;
                }
                uiPanel03.Refresh();

                if (queue[3].Status == "ok")
                {
                    uiPanel04.BackColor = Color.LightGreen;
                    uiPanel04.FillColor = Color.LightGreen;
                }
                else if (queue[3].Status == "work")
                {
                    uiPanel04.BackColor = Color.Yellow;
                    uiPanel04.FillColor = Color.Yellow;
                }
                else if (queue[3].Status == "unok")
                {
                    uiPanel04.BackColor = Color.Red;
                    uiPanel04.FillColor = Color.Red;
                }
                else if (queue[3].Status == "pass")
                {
                    uiPanel04.BackColor = Color.LightGray;
                    uiPanel04.FillColor = Color.LightGray;
                }
                else
                {
                    uiPanel04.BackColor = Color.White;
                    uiPanel04.FillColor = Color.White;
                }
                uiPanel04.Refresh();

                if (queue[4].Status == "ok")
                {
                    uiPanel05.BackColor = Color.LightGreen;
                    uiPanel05.FillColor = Color.LightGreen;
                }
                else if (queue[4].Status == "work")
                {
                    uiPanel05.BackColor = Color.Yellow;
                    uiPanel05.FillColor = Color.Yellow;
                }
                else if (queue[4].Status == "unok")
                {
                    uiPanel05.BackColor = Color.Red;
                    uiPanel05.FillColor = Color.Red;
                }
                else if (queue[4].Status == "pass")
                {
                    uiPanel05.BackColor = Color.LightGray;
                    uiPanel05.FillColor = Color.LightGray;
                }
                else
                {
                    uiPanel05.BackColor = Color.White;
                    uiPanel05.FillColor = Color.White;
                }
                uiPanel05.Refresh();
                ****************/
            }));
        }

        public void Dgv_Cells(ref UIDataGridView dgv)
        {
            if (dgv.Rows.Count > 0)
                dgv.Rows[0].Selected = false;

            foreach (DataGridViewRow item in dgv.Rows)
            {
                var model = item.DataBoundItem as QueueInfoModel;
                if (model.Status == "ok")
                {
                    item.DefaultCellStyle.BackColor = Color.LightGreen;
                }
                else if (model.Status == "work")
                {
                    item.DefaultCellStyle.BackColor = Color.Yellow;
                }
                else if (model.Status == "unok")
                {
                    item.DefaultCellStyle.BackColor = Color.Red;
                }
                else if (model.Status == "pass")
                {
                    item.DefaultCellStyle.BackColor = Color.LightGray;
                }
                else
                {
                    item.DefaultCellStyle.BackColor = Color.White;
                }
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            try
            {
                var model = pickingGuideHandler.GetWorkInfoToWeb(1);
                if (model != null)
                {
                    var racks = pickingGuideHandler.GetRackList(model, Station);
                    if (racks != null && racks.Count > 0)
                    {

                        WorkGuideRefresh(model);
                        PanelRefresh(racks);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.timer1.Stop();
            this.Close();
        }
    }
}
